import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.time.LocalTime;

import static java.awt.Font.DIALOG;

public class TimeDemo extends JDialog{
    JLabel label1;
    JLabel label2;
    TrayIcon trayIcon = null; // 托盘图标
    public TimeDemo(){
        setUndecorated(true);
        setBackground(new Color(0,0,0,0));
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        setSize(d.width, d.height);
        setLayout(null);


        label1 = new JLabel("");
        label2 = new JLabel("");
        label1.setForeground(new Color(255,255,255));
        label2.setForeground(new Color(255,255,255));
        label1.setBounds(0,0,500,100);
        label2.setBounds(0,100,500,100);
        label1.setFont(new java.awt.Font(DIALOG, 1, 100));
        label2.setFont(new java.awt.Font(DIALOG, 1, 40));

        getContentPane().add(label1);
        getContentPane().add(label2);

        ImageIcon arrowIcon = null;
        java.net.URL imgURL = TimeDemo.class.getResource("src/main/resources/image.jpg");
        if (imgURL != null) {
            arrowIcon = new ImageIcon(imgURL);
            setIconImage(arrowIcon.getImage());
        } else {
            JOptionPane.showMessageDialog(this, "Icon image not found.");
        }



        //拖拽窗口
        label1.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                label1.setLocation(e.getX()+label1.getX(),e.getY()+label1.getY());//面板位置随鼠标拖动变化，
                label2.setLocation(e.getX()+label2.getX(),e.getY()+label2.getY());//面板位置随鼠标拖动变化，
                System.out.println(e.getX()+","+e.getY());
            }
        });
        //拖拽窗口
        label2.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {

                label1.setLocation(e.getX()+label1.getX(),e.getY()+label1.getY());//面板位置随鼠标拖动变化，
                label2.setLocation(e.getX()+label2.getX(),e.getY()+label2.getY());//面板位置随鼠标拖动变化，
                System.out.println(e.getX()+","+e.getY());
            }
        });


        SystemTray tray = SystemTray.getSystemTray(); // 获得本操作系统托盘的实例
        ImageIcon icon = new ImageIcon(imgURL);
        PopupMenu pop = new PopupMenu(); // 构造一个右键弹出式菜单
        MenuItem exit = new MenuItem("exit");
        pop.add(exit);
        trayIcon = new TrayIcon(icon.getImage(), "时钟", pop);
        exit.addActionListener(new ActionListener() // 点击“退出演示”菜单后退出程序
        {
            public void actionPerformed(ActionEvent e) {
                System.exit(0); // 退出程序
            }
        });
        try {
            tray.add(trayIcon); // 将托盘图标添加到系统的托盘实例中
        } catch (AWTException e) {
            e.printStackTrace();
        }

        //更新时间
        TimePrinter printer = new TimePrinter(label1);
        Thread thread1 = new Thread(printer);
        thread1.start();
        //更新天气
        WeatherPrinter weatherPrinter = new WeatherPrinter(label2);
        Thread thread2 = new Thread(weatherPrinter);
        thread2.start();
    }



    public static void main(String[] args) {
        TimeDemo timeDemo = new TimeDemo();
        timeDemo.setVisible(true);

    }
}